﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    class RegimenLaboralRepository: MasterRepository, IRegimenLaboralRepository
    {
        public List<REGIMEN_LABORAL> GetAllFromRegimenLaboral()
        {
            var query = from p in context.REGIMEN_LABORAL
                        select p;

            return query.ToList();
        }

        public List<REGIMEN_LABORAL> GetAllFromRegimenLaboralByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.REGIMEN_LABORAL
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;

                return query.ToList();
            }
            else
            {
                var query = from p in context.REGIMEN_LABORAL
                            select p;

                return query.ToList();
            }
        }

        public REGIMEN_LABORAL GetFromRegimenLaboralById(int id)
        {
            var query = from p in context.REGIMEN_LABORAL
                        where p.IdRegimenLaboral.Equals(id)
                        select p;
            return query.SingleOrDefault();
        }

        public void AddRegimenLaboral(REGIMEN_LABORAL regLab)
        {
            regLab.CONTRATOS = null;
            context.REGIMEN_LABORAL.Add(regLab);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateRegimenLaboral(REGIMEN_LABORAL regLab)
        {
            regLab.CONTRATOS = null;
            context.Entry(regLab).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void DeleteRegimenLaboral(int id)
        {
            var existe = context.REGIMEN_LABORAL.Where(p => p.IdRegimenLaboral.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                context.REGIMEN_LABORAL.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool existeDuplicidad(string descripcion, string codigo, Int32 id)
        {
            var query = from p in context.REGIMEN_LABORAL
                        where (p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        || p.Codigo.ToUpper().CompareTo(codigo.ToUpper())==0)
                        && p.IdRegimenLaboral != id
                        select p;

            return query.Any();
        }


        public REGIMEN_LABORAL GetFromRegimenLaboralByCodigo(string codigo)
        {
            var query = from ds in context.REGIMEN_LABORAL
                        where ds.Codigo.Equals(codigo)
                        select ds;
            return query.SingleOrDefault();
        }
    }
}
